package comparison.sort;

import comparison.sort.algorithm.QuickSort;
import comparison.sort.algorithm.SelectionSort;
import comparison.sort.algorithm.ShellSort;
import comparison.sort.algorithm.Sort;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class DataSortingActivity extends Activity implements OnClickListener {

	private static final int SELECTIONSORT = R.id.algorithm1;
	private static final int QUICKSORT=R.id.algorithm2;
	private static final int SHELLSORT=R.id.algorithm3;
	
	private Button next;
	private TextView nameAlgoSort,numberData,showData ;
	private int algorithm;
	private Sort algoSort;
	private int numberOfData;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_viewdata);
		next = (Button)findViewById(R.id.next);
		nameAlgoSort = (TextView)findViewById(R.id.tVnameAlgoSort2);
		numberData = (TextView)findViewById(R.id.showNumberOfData);
		showData = (TextView)findViewById(R.id.showData);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		//Get algorithm chosen
		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		algorithm =bundle.getInt("ALGORITHM");
		numberOfData = bundle.getInt("NUMBERDATA");
		
		//Show mesg algorithm chosen to screen 
		switch (algorithm) {
		case SELECTIONSORT:
			nameAlgoSort.setText("The Selection Sort is chosen");
			algoSort = new SelectionSort(numberOfData);
			break;
		case QUICKSORT:
			nameAlgoSort.setText("The Quick Sort is chosen");
			algoSort = new QuickSort(numberOfData);
			break;
		case SHELLSORT:
			nameAlgoSort.setText("The Shell Sort is chosen");
			algoSort = new ShellSort(numberOfData);
			break;
		default:
			break;
		}
		numberData.setText("Choose array for sorting. The are "+numberOfData+" number");
		showData.setText(algoSort.stringData().substring(1, algoSort.stringData().length()-1));	
		next.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.next:
			algoSort.run(0, algoSort.numberOfData()-1);
			Intent intent = new Intent("comparison.sort.LOGSORTING");
			Bundle bundle = new Bundle();
			bundle.putInt("ALGORITHM", algorithm);
			bundle.putString("LOGSORTING", algoSort.logSort());
			intent.putExtras(bundle);
			startActivity(intent);
			break;

		default:
			break;
		}
	}

}
